//
//  AlertAction.swift
//  SimpleAlert
//
//  Created by Kyohei Ito on 2016/09/12.
//  Copyright © 2016年 kyohei_ito. All rights reserved.
//

import UIKit

open class AlertAction {
    public typealias Style = UIAlertAction.Style

    public init(title: String, style: Style = .default, shouldDismisses: Bool = true, handler: ((AlertAction) -> Void)? = nil) {
        self.title = title
        self.handler = handler
        self.style = style
        self.shouldDismisses = shouldDismisses

        button.setTitle(title, for: .normal)
        button.setBackgroundImage(UIImage(color: .lightGray), for: .highlighted)
    }
    
    public init(title: NSAttributedString, style: Style = .default, shouldDismisses: Bool = true, handler: ((AlertAction) -> Void)? = nil) {
        self.attributedTitle = title
        self.handler = handler
        self.style = style
        self.shouldDismisses = shouldDismisses

        button.setAttributedTitle(title, for: .normal)
        button.setBackgroundImage(UIImage(color: .lightGray), for: .highlighted)
    }

    var title: String?
    var attributedTitle: NSAttributedString?
    let handler: ((AlertAction) -> Void)?
    let style: Style
    let shouldDismisses: Bool
    public let button = UIButton(type: .system)
    public var isEnabled: Bool  {
        get { return button.isEnabled }
        set { button.isEnabled = newValue }
    }
}
extension AlertAction.Style {
    func font(of style: UIAlertController.Style) -> UIFont {
        switch self {
        case .cancel:
            return UIFont.lxs.font(size: style.fontSize)
        default:
            return .systemFont(ofSize: style.fontSize)
        }
    }
}
